﻿using MicroBlog.Areas.Admin.ViewModels;
using MicroBlog.Infrastructure;
using MicroBlog.Services;
using Microsoft.AspNetCore.Mvc;

namespace MicroBlog.Areas.Admin.Controllers
{
    public class UserController : BaseController
    {
        private readonly IUserManager _userManager;

        public UserController(IUserManager userManager)
        {
            _userManager = userManager;
        }

        public IActionResult Index()
        {
            return View();
        }

        public async Task<UserModel> GetUser()
        {
            var result = await _userManager.GetCurrentUser(HttpContext);
            return result;
        }
        public async Task<IActionResult> Update([FromBody] UserModel user)
        {
            if (ModelState.IsValid)
            {
                var result = await _userManager.ChangeSecret(user.UserId, user.OldSecret, user.Secret);
                return Json(result);
            }
            else
            {
                ContextResult result = ContextResult.Failed(new ContextError
                {
                    Code = "400",
                    Description = GetModelErrors(ModelState)
                });
                return Json(result);
            }
        }
    }
}